//
//  NSArray+category.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/24.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "NSArray+category.h"

@implementation NSArray (category)
//求和值
+ (int)sum:(NSArray *)array{
    return [[array valueForKeyPath:@"@sum.intValue"] intValue];
}

//求差值
+ (int)minus:(NSArray *)array{
    return [self max:array] - [self min:array];
}

//求平均值
+ (int)avg:(NSArray *)array{
    return [[array valueForKeyPath:@"@avg.intValue"] intValue];
}

//最大值
+ (int)max:(NSArray *)array{
    return [[array valueForKeyPath:@"@max.intValue"] intValue];
}

//最小值
+ (int)min:(NSArray *)array{
    return [[array valueForKeyPath:@"@min.intValue"] intValue];
}

+ (NSArray *)ascendingArray:(NSArray *)array{
    NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(NSString * obj1, NSString * obj2) {
        if ([obj1 intValue] < [obj2 intValue])
            return NSOrderedAscending;//升序
        else if ([obj1 intValue] > [obj2 intValue])
            return NSOrderedDescending;//降序
        else
            return NSOrderedSame;
    }];
    return result;
}

- (int)sum{
    return [NSArray sum:self];
}

- (int)minus{
    return [NSArray minus:self];
}

- (int)avg{
    return [NSArray avg:self];
}

- (int)max{
    return [NSArray max:self];
}

- (int)min{
    return [NSArray min:self];
}

- (NSString *)sameString{
   NSSet *set = [NSSet setWithArray:self];
    NSUInteger count = set.count;
    if (count == 1) return @"三同号";
    else if (count == 2) return @"二同号";
    else if (count == 3)  return @"三不同号";
    else return nil;
}
@end
